import axios from "axios";
import { showToast } from "vant";
import JSONBig from "json-bigint";
import { detectPlatform } from "../utils/isMobile";
import baseURL from "./baseURL.js";
import { eventBus } from "@/utils/eventBus";
import i18n from "@/lang";

const service = axios.create({
  // baseURL: import.meta.env.VITE_APP_BASE_KH_URL + window.location.hostname.slice(window.location.hostname.indexOf(".")),
  // baseURL: import.meta.env.VITE_APP_BASE_URL,
  baseURL: baseURL,
  timeout: 6000,
  transformResponse: [
    function (data) {
      // 对 data 进行任意转换处理
      try {
        return JSONBig.parse(data);
      } catch (err) {
        return data;
      }
    }
  ]
});

service.interceptors.request.use(
  config => {
    let token;
    switch (config.url) {
      default:localStorage
        token = JSON.parse(localStorage.getItem("token"))?.token;
        break;
    }
    // 读取缓存的语言 -- 接口多语言
    config.headers["lang"] = localStorage.getItem("lang");
    // if (detectPlatform() == "iOS") config.headers["Device"] = "ios";
    // if (detectPlatform() == "Android") config.headers["Device"] = "android";
    // console.log(detectPlatform());
    if (detectPlatform() == "Windows") config.headers["Device-type"] = "pc";
    if (detectPlatform() != "Windows") config.headers["Device-type"] = "h5";
    if (token) {
      config.headers["Access-Token"] = token;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

// 用户信息失效
const tokenErrCode = [20005, 10015, 10204, 10405, 10404]

const systemError = [10001, 10002, 10004, 10005, 10006, 
  10007, 10008, 10009, 10010, 10011, 10012, 10013, 10014,
   10101, 10102, 10103, 10104, 10105, 10106, 10107, 10201, 
   10202, 10203, 10204, 10301, 10302, 10303, 10304, 10305, 20001, 
   20002, 20003, 20004, 20005, 20006, 20007, 20008, 20009, 20010,
   20012, 20013, 20013, 20014, 20015, 20016, 20017, 20019, 20020,
   20024, 20101, 20102, 20103, 20104, 20105, 20107, 20108, 20110,
   20111, 20112, 20701, 20702, 20703, 20704, 20705, 20706, 20707,
   20708, 20906, 20907,
  ]


let showToastIng = false

const showTokenErrorNotification = () => {
  showToastIng = true;

  setTimeout(() => {
    ElNotification({
      position: "top-left",
      zIndex: 1004,
      message: i18n.global.t('layer.code.401'),
      type: "error"
    });
    showToastIng = false;
  }, 2000)
}; 


service.interceptors.response.use(
  res => {
    // console.log(res.data);
    //响应拦截 20005 == token失效
    if (tokenErrCode.includes(res.data.errno)) {
      //清缓存
      let lang = localStorage.getItem("lang");
      sessionStorage.removeItem("user");
      sessionStorage.removeItem("vipinfo");
      localStorage.clear();
      eventBus.emit("clearToken");
      localStorage.setItem("lang", lang); 
      if(!showToastIng) {
        showTokenErrorNotification();
      }
      return res.data;
    } else if (systemError.includes(res.data.errno)) {
      ElNotification({
        position: "top-left",
        zIndex: 1015,
        message: i18n.global.t('layer.code.systemError'),
        type: "error"
      });
      
    }  else if (res.data.errno != 0 && res.data.errno  != 20908) {
      // alterMsg(res.data.errno)
      let str = i18n.global.t(`${'layer.code.'+ res.data.errno}`)
      ElNotification({
        position: "top-left",
        zIndex: 1015,
        message: str,
        type: "error"
      }); 
    }

    return res.data;
  },
  error => { 
    console.log(error); 
    if (error.response?.status == 401) {
      // clear
      // sessionStorage.clear();
      // location.reload();
    }
  }
);



const alterMsg = (code) => {
  let str = ''
  switch (code) {
    case 10401:
        str = i18n.global.t('layer.code.10401')
    break;
    case 10402:
      str = i18n.global.t('layer.code.10402')
    break;
    case 10403:
      str = i18n.global.t('layer.code.10403')
    break;
    case 10404:
      str = i18n.global.t('layer.code.10404')
    break;
    case 10405:
      str = i18n.global.t('layer.code.10405')
    break;
    case 10406:
      str = i18n.global.t('layer.code.10406')
    break;
    case 10407:
      str = i18n.global.t('layer.code.10407')
    break;
    case 10408:
      str = i18n.global.t('layer.code.10408')
    break;
    case 10409:
      str = i18n.global.t('layer.code.10409')
    break;
    case 10501:
        str = i18n.global.t('layer.code.10501')
    break;
    case 10601:
      str = i18n.global.t('layer.code.10601')
    break;
    case 10602:
      str = i18n.global.t('layer.code.10602')
    break;
    case 10603:
      str = i18n.global.t('layer.code.10603')
    break;
    case 10604:
      str = i18n.global.t('layer.code.10604')
    break;
    case 10605:
      str = i18n.global.t('layer.code.10605')
    break;
    case 10606:
      str = i18n.global.t('layer.code.10606')
    break;
    case 10607:
      str = i18n.global.t('layer.code.10607')
    break;
    case 10608:
      str = i18n.global.t('layer.code.10608')
    break;
    case 10609:
      str = i18n.global.t('layer.code.10609')
    break;
    case 10610:
      str = i18n.global.t('layer.code.10610')
    break;
    case 10611:
      str = i18n.global.t('layer.code.10611')
    break;
    case 10612:
      str = i18n.global.t('layer.code.10612')
    break;
    case 10613:
      str = i18n.global.t('layer.code.10613')
    break;
    case 10614:
      str = i18n.global.t('layer.code.10614')
    break;
    case 10615:
      str = i18n.global.t('layer.code.10615')
    break;
    case 10617:
      str = i18n.global.t('layer.code.10617')
    break;
    case 10618:
      str = i18n.global.t('layer.code.10618')
    break;
    case 10619:
      str = i18n.global.t('layer.code.10619')
    break;
    case 20011:
      str = i18n.global.t('layer.code.20011')
    break;
    case 20018:
      str = i18n.global.t('layer.code.20018')
    break;
    case 20021:
      str = i18n.global.t('layer.code.20021')
    break;
    case 20022:
      str = i18n.global.t('layer.code.20022')
    break;
    case 20023:
      str = i18n.global.t('layer.code.20023')
    break;
    case 20025:
      str = i18n.global.t('layer.code.20025')
    break;
    case 20026:
      str = i18n.global.t('layer.code.20026')
    break;
    case 20027:
      str = i18n.global.t('layer.code.20027')
    break;
    case 20028:
      str = i18n.global.t('layer.code.20028')
    break;
    case 20109:
      str = i18n.global.t('layer.code.20109')
    break;
    case 20301:
      str = i18n.global.t('layer.code.20301')
    break;
    case 20302:
      str = i18n.global.t('layer.code.20302')
    break;
    case 20303:
      str = i18n.global.t('layer.code.20303')
    break;
    case 20304:
      str = i18n.global.t('layer.code.20304')
    break;
    case 20305:
      str = i18n.global.t('layer.code.20305')
    break;
    case 20306:
      str = i18n.global.t('layer.code.20306')
    break;
    case 20307:
      str = i18n.global.t('layer.code.20307')
    break;
    case 20308:
      str = i18n.global.t('layer.code.20308')
    break;
    case 20401:
      str = i18n.global.t('layer.code.20401')
    break;
    case 20402:
      str = i18n.global.t('layer.code.20402')
    break;
    case 20403:
      str = i18n.global.t('layer.code.20403')
    break;
    case 20404:
      str = i18n.global.t('layer.code.20404')
    break;
    case 20405:
      str = i18n.global.t('layer.code.20405')
    break;
    case 20406:
      str = i18n.global.t('layer.code.20406')
    break;
    case 20407:
      str = i18n.global.t('layer.code.20407')
    break;
    case 20408:
      str = i18n.global.t('layer.code.20408')
    break;
    case 20409:
      str = i18n.global.t('layer.code.20409')
    break;
    case 20410:
      str = i18n.global.t('layer.code.20410')
    break;
    case 20411:
      str = i18n.global.t('layer.code.20411')
    break;
    case 20412:
      str = i18n.global.t('layer.code.20412')
    break;
    case 20413:
      str = i18n.global.t('layer.code.20413')
    break;
    case 20414:
      str = i18n.global.t('layer.code.20414')
    break;
    case 20415:
      str = i18n.global.t('layer.code.20415')
    break;
    case 20416:
      str = i18n.global.t('layer.code.20416')
    break;
    case 20417:
      str = i18n.global.t('layer.code.20417')
    break;
    case 20418:
      str = i18n.global.t('layer.code.20418')
    break;
    case 20419:
      str = i18n.global.t('layer.code.20419')
    break;
    case 20420:
      str = i18n.global.t('layer.code.20420')
    break;
    case 20421:
      str = i18n.global.t('layer.code.20421')
    break;
    case 20422:
      str = i18n.global.t('layer.code.20422')
    break;
    case 20423:
      str = i18n.global.t('layer.code.20423')
    break;
    case 20424:
      str = i18n.global.t('layer.code.20424')
    break;
    case 20425:
      str = i18n.global.t('layer.code.20425')
    break;
    case 20426:
      str = i18n.global.t('layer.code.20426')
    break;
    case 20427:
      str = i18n.global.t('layer.code.20427')
    break;
    case 20501:
      str = i18n.global.t('layer.code.20501')
    break;
    case 20502:
      str = i18n.global.t('layer.code.20502')
    break;
    case 20503:
      str = i18n.global.t('layer.code.20503')
    break;
    case 20504:
      str = i18n.global.t('layer.code.20504')
    break;
    case 20601:
      str = i18n.global.t('layer.code.20601')
    break;
    case 20602:
      str = i18n.global.t('layer.code.20602')
    break;
    case 20603:
      str = i18n.global.t('layer.code.20603')
    break;
    case 20604:
      str = i18n.global.t('layer.code.20604')
    break;
    case 20605:
      str = i18n.global.t('layer.code.20605')
    break;
    case 20606:
      str = i18n.global.t('layer.code.20606')
    break;
    case 20901:
      str = i18n.global.t('layer.code.20901')
    break;
    case 20902:
      str = i18n.global.t('layer.code.20902')
    break;
    case 20903:
      str = i18n.global.t('layer.code.20903')
    break;
    case 20904:
      str = i18n.global.t('layer.code.20904')
    break;
    // case 20908:
    //   str = i18n.global.t('layer.code.20908')
    // break;
    case 20909:
      str = i18n.global.t('layer.code.20909')
    break;
    case 20910:
      str = i18n.global.t('layer.code.20910')
    break;
    case 20911:
      str = i18n.global.t('layer.code.20911')
    break;
    case 20912:
      str = i18n.global.t('layer.code.20912')
    break;
    case 20913:
      str = i18n.global.t('layer.code.20913')
    break;
    case 20914:
      str = i18n.global.t('layer.code.20914')
    break;
    case 20309:
      str = i18n.global.t('layer.code.20309')
    break;
    case 20431:
      str = i18n.global.t('layer.code.20431')
    break;
    case 20432:
      str = i18n.global.t('layer.code.20432')
    break;
    case 20433:
      str = i18n.global.t('layer.code.20433')
    break;
    case 20203:
      str = i18n.global.t('layer.code.20203')
    break;
    case 20428:
      str = i18n.global.t('layer.code.20428')
    break;
    case 10003:
      str = i18n.global.t('layer.code.10003')
    break;
    case 10016:
      str = i18n.global.t('layer.code.10016')
    break;
    
  }
  ElNotification({
    position: "top-left",
    zIndex: 1015,
    message: str,
    type: "error"
  }); 
}
export default service;
